/*
 * CDNBitStreamExtract.cpp
 *
 *  Created on: Apr 5, 2012
 *      Author: nikhil
 */

#include "CDNBitStreamExtract.h"
#include "BStreamExtractor.h"

//#ifndef __EXTRACTOR_H_D65BE9B4_A8DA_11D3_AFE7_005004464B79
//#define __EXTRACTOR_H_D65BE9B4_A8DA_11D3_AFE7_005004464B79
#include "Extractor.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include <sstream>

#define argvCounter 10
using namespace std;


CDNBitStreamExtract::CDNBitStreamExtract()
{
	iNumLayersMinus1=0;
}

int CDNBitStreamExtract:: SelectLayerIdstoExtract(char *hdfsFileName)
{

	Extractor*          pcExtractor = NULL;
	ExtractorParameter  cParameter;
	int argc;
	char * argv[argvCounter];


     argc =2;
     argv[0]=NULL;
 	//Assign HDFS file name as an command line Argument
     argv[1]=hdfsFileName;

	 cerr<<"JSVM %s BitStream Extractor \n\n"<<endl;

	 cerr<<"File name specified using argv is :"<<argv[1]<<endl;

	  RNOKRS( cParameter.init       ( argc, argv ),   -2 );

	  for( Int n = 0; n < 1; n++ )
	  {
	    RNOKR( Extractor::create    ( pcExtractor ),  -3 );

	    RNOKR( pcExtractor->init    ( &cParameter ),  -4 );

	    RNOKR( pcExtractor->go      (),               -5 );

	    iNumLayersMinus1 = pcExtractor->getExtNumLayersMinus1();
	    for (UInt i = 0; i<=iNumLayersMinus1; i++)
	    {
	    	iLayerIds[i] = pcExtractor->getExtLayerId(i);
	    }

	    cerr<<"Layer ids Selected are :"<<endl;
	    for(UInt i = 0; i<=iNumLayersMinus1; i++)
	    {
	    	printf("%3d\n",iLayerIds[i]);
	    }

	    cerr<<endl;
	    RNOKR( pcExtractor->destroy (),               -6 );
	  }

	  return 0;

}



char* CDNBitStreamExtract::getOutputFileName(char* ip, UInt ilayerId)
{
		string sip(ip);
		int ipos = sip.rfind('.');
		int ilength = sip.length();

		string sop = sip.replace(ipos, (ilength - ipos), "");

		char slayerid[10];
		sprintf(slayerid, "_%d.svc", ilayerId);

		sop.append(slayerid);

		return (char*)sop.c_str();

}

int CDNBitStreamExtract::simpleRun(char *hdfsFileName)
{

	int argc;
	char * argv[argvCounter];
	Extractor*          pcExtractor = NULL;
	ExtractorParameter  cParameter;

	string hostnames[]={"localhost","localhost","localhost"};
	string portnos[]={"6001","6002","6003"};

	argv[0]=NULL;
	//Assign HDFS file name as an command line Argument
	argv[1]=hdfsFileName;

	cerr<<"iNumLayersMinus1 is :"<<iNumLayersMinus1<<endl;
	 cerr<<"Layer ids printed from simpleRun  Selected are :"<<endl;
		    for(UInt i = 0; i<=iNumLayersMinus1; i++)
		    {
		    	//printf("%3d\n",iLayerIds[i]);
		    	cerr<<iLayerIds[i]<<endl;
		    }


	for (int iCtr = 0; iCtr<=iNumLayersMinus1; iCtr++)
	  {
		  cerr<<"input file name :"<<argv[1]<<endl;

		  argv[2] = getOutputFileName(argv[1], iLayerIds[iCtr]);
		  cerr<<"output file name :"<<argv[2]<<endl;
		  argv[3] = "-sl";

		  cerr<<"ilayer 1"<<endl;
		  argv[4]= (char *) malloc(sizeof(int));
		  sprintf(argv[4], "%d", iLayerIds[iCtr]);
		  cerr<<"ilayer 2"<<endl;
		  cerr<<"argv[4] is :"<<argv[4]<<endl;

		  argc = 5;

		  pcExtractor = NULL;
		  //cParameter = NULL;

		 // printf( "Calling again \n\n");

		  cerr<<"cParameter.init sstarted"<<endl;
		  RNOKRS( cParameter.init       ( argc, argv ),   -2 );

		  cerr<<"cParameter.init Ended"<<endl;

		  for( Int n = 0; n < 1; n++ )
		  {
			  cerr<<"trig 78"<<endl;
			RNOKR( Extractor::create    ( pcExtractor ),  -3 );
			  cerr<<"trig 79"<<endl;

			cParameter.hostname=(char *)hostnames[iCtr].c_str();
			cParameter.portno=(char *)portnos[iCtr].c_str();
			cerr<<"cParameter hostname is :"<<cParameter.hostname<<endl;
			cerr<<"cParameter portno is :"<<cParameter.portno<<endl;

			  cerr<<"trig 80"<<endl;

			RNOKR( pcExtractor->init    ( &cParameter ),  -4 );

			RNOKR( pcExtractor->go      (),               -5 );

			RNOKR( pcExtractor->destroy (),               -6 );
		  }

	  }

	return 0;
}

int CDNBitStreamExtract::processLayer(int VendorID, char* hdfsfileName, int layerID, int totalLayers, char* edgeserver_host, int edgeserver_port)
{

			int argc;
			char * argv[argvCounter];
			Extractor*          pcExtractor = NULL;
			ExtractorParameter  cParameter;

			  cerr<<"input file name :"<<hdfsfileName<<endl;
			  argv[1]=hdfsfileName;
			  //argv[2] = getOutputFileName(argv[1],layerID);
			  argv[2]=argv[1];
			  cerr<<"output file name :"<<argv[2]<<endl;
			  argv[3] = "-sl";

			  cerr<<"ilayer 1"<<endl;
			  argv[4]= (char *) malloc(sizeof(int));
			  sprintf(argv[4], "%d", layerID);
			  cerr<<"ilayer 2"<<endl;
			  cerr<<"argv[4] is :"<<argv[4]<<endl;

			  argc = 5;

			  pcExtractor = NULL;
			  //cParameter = NULL;

			 // printf( "Calling again \n\n");

			  cerr<<"cParameter.init sstarted"<<endl;
			  RNOKRS( cParameter.init       ( argc, argv ),   -2 );

			  cerr<<"cParameter.init Ended"<<endl;


			  //Convert from integer to string
			  std::string strPort;
			  std::stringstream strStreamOut;
			  strStreamOut << edgeserver_port;
			  strPort = strStreamOut.str();

			  cerr<<"Port number is :"<<strPort;
			  for( Int n = 0; n < 1; n++ )
			  {
				  cerr<<"trig 78"<<endl;
				RNOKR( Extractor::create    ( pcExtractor ),  -3 );
				  cerr<<"trig 79"<<endl;

				cParameter.hostname=edgeserver_host;
				cParameter.portno=(char *)strPort.c_str();
				cParameter.vendorID=VendorID;

				//nchange
				cParameter.layerID=layerID;
				cerr<<"cParameter hostname is :"<<cParameter.hostname<<endl;
				cerr<<"cParameter portno is :"<<cParameter.portno<<endl;

				cerr<<"trig 80"<<endl;

				RNOKR( pcExtractor->init    ( &cParameter ),  -4 );

				RNOKR( pcExtractor->go      (),               -5 );

				RNOKR( pcExtractor->destroy (),               -6 );
			  }

			  // return value is greater than zero
			  return 10;


}

